<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :listType="props.listType" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
      >
        <template #toolbar="{}">
          <el-button type="success" @click="openEdit(null)" :icon="Plus">
            账单录入
          </el-button>
          <el-button type="info" @click="exportExcel" :icon="DocumentCopy">
            导出表格
          </el-button>
        </template>
        <template #deadline="{ row }">
          {{ row.deadline }}<br />
          <span class="red"> {{ row.deadlineDay }} </span>
        </template>
        <template #status="{ row }">
          <el-tag :type="statusType(row.repay_status)">
            {{ statusText(row.repay_status) }}
          </el-tag>
        </template>
        <template #repay_amount="{ row }">
          <span class="pointer blue" @click="openRepayRecord(row)">{{
            row.repay_amount
          }}</span>
        </template>
        <template #deduction_amount="{ row }">
          <span v-if="row.deduction_amount == '无'">无</span>
          <span v-else class="pointer blue" @click="openPrepay(row)">{{
            row.deduction_amount
          }}</span>
        </template>
        <template #huikuan="{ row }">
          {{ row.old_huikuan1 }}
          {{ row.old_huikuan2 }}
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button type="primary" size="small" @click="openRepay(row)">
              还款
            </el-button>
            <!-- <el-button type="info" size="small" @click="openDetail(row)">
              详情
            </el-button> -->
            <el-button type="warning" size="small" @click="openEdit(row)">
              编辑
            </el-button>
            <!-- <el-button type="danger" size="small" @click="handleDelete(row)">
              删除
            </el-button> -->
          </el-button-group>
        </template>
      </ele-pro-table>
      <!-- 账单详情 -->
      <detail v-model="showDetail" :data="current" />
      <!-- 还款操作 -->
      <repay v-model="showRepay" :data="current" @done="reload" />
      <!-- 还款记录 -->
      <repay-record v-model="showRepayRecord" :data="current" />
      <!-- 账单录入 -->
      <edit v-model="showEdit" :data="current" />
    </ele-card>
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Edit from './components/edit.vue';
  import Search from './components/search.vue';
  import Detail from './components/detail.vue';
  import Repay from './components/repay.vue';
  import RepayRecord from './components/repay-record.vue';
  import { DocumentCopy, Plus } from '@element-plus/icons-vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { pageBillList, exportBillList } from '@/api/company';

  const props = defineProps({
    /** 弹窗是否打开 */
    listType: {
      type: String,
      default: '全部'
    }
  });

  const statusText = (value) => {
    const cs = {
      1: '已还清',
      0: '待还款'
    };
    return cs[value];
  };
  const statusType = (state) => {
    const stateMap = {
      1: 'success',
      0: 'warning'
    };
    return stateMap[state];
  };

  /** 表格实例 */
  const tableRef = ref(null);

  /** 默认条件 */
  const defaultWhere = {
    repay_status: 0,
    listType: props.listType
  };

  console.log(props.listType);
  /** 表格列配置 */
  const columns = ref([
    // {
    //   type: 'selection',
    //   columnKey: 'selection',
    //   width: 50,
    //   align: 'center',
    //   fixed: 'left',
    //   selectable: (row) => {
    //     // 禁用 id 为 45、47、48 的数据的复选框
    //     return row.id > 0;
    //   }
    // },
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },

    {
      width: 110,
      align: 'center',
      slot: 'deadline',
      label: '到期还款日'
    },
    {
      width: 70,
      align: 'center',
      prop: 'cate',
      label: '类型'
    },
    {
      width: 100,
      prop: 'old_lixi',
      label: '旧表利息'
    },
    {
      width: 100,
      slot: 'huikuan',
      label: '旧表回款'
    },
    {
      width: 120,
      prop: 'grandfather',
      label: '还款人'
    },
    {
      width: 120,
      prop: 'grandfather_tel',
      label: '联系电话'
    },
    {
      width: 120,
      prop: 'plate_number',
      label: '所属车辆'
    },
    {
      width: 100,
      prop: 'totalAmount',
      label: '总金额'
    },
    {
      width: 100,
      slot: 'repay_amount',
      label: '已收'
    },
    {
      width: 100,
      slot: 'deduction_amount',
      label: '减免'
    },
    {
      width: 100,
      slot: 'status',
      label: '状态'
    },
    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      fixed: 'right'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageBillList({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 账单详情 */
  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row;
    showDetail.value = true;
  };

  /** 还款操作 */
  const showRepay = ref(false);
  const openRepay = (row) => {
    current.value = row;
    showRepay.value = true;
  };
  /** 导出表格 */
  const exportExcel = () => {
    const loading = EleMessage.loading('请求中..');
    tableRef.value?.fetch(({ where }) => {
      // 方法的参数与数据源的完全一致
      // 请求查询全部接口
      console.log(where);
      var requestData = where;
      requestData.limit = 99999999;
      requestData.page = 1;

      exportBillList(requestData)
        .then((res) => {
          loading.close();
          window.open(res);
        })
        .catch(() => {
          loading.close();
        });
    });
  };

  /** 账单录入 */
  const showEdit = ref(false);
  const openEdit = (row) => {
    if (row) {
      if (row.cate == '分期' || row.cate == '垫付') {
        EleMessage.warning(row.cate + '账单不能修改');
        return;
      }
    }

    current.value = row;
    showEdit.value = true;
  };

  /** 还款记录 */
  const showRepayRecord = ref(false);
  const openRepayRecord = (row) => {
    current.value = row;
    showRepayRecord.value = true;
  };

  /** 提前还款详情 */
  const showPrepay = ref(false);
  const openPrepay = (row) => {
    current.value = row;
    showPrepay.value = true;
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };
</script>
